﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.TransientFaultHandling;

namespace IEx.Common
{
    public sealed class WebRequestTransientStrategy : ITransientErrorDetectionStrategy
    {

        public bool IsTransient(Exception ex)
        {
            if (ex != null)
            {
                WebException webEx;
                if ((webEx = (ex as WebException)) != null)
                {
                    switch (webEx.Status)
                    {
                        case WebExceptionStatus.ConnectFailure:
                        case WebExceptionStatus.ReceiveFailure:
                        case WebExceptionStatus.KeepAliveFailure:
                        case WebExceptionStatus.Timeout:
                        case WebExceptionStatus.UnknownError:
                        case WebExceptionStatus.NameResolutionFailure:
                        case WebExceptionStatus.ProxyNameResolutionFailure:
                            Logger.Debug("REQUEST RETRY ERROR: " + ex.ToString());
                            return true;
                    }
                }
            }

            return false;
        }
    }
    public class WebRequestRetryPolicy : RetryPolicy
    {
        public WebRequestRetryPolicy()
            : base(new WebRequestTransientStrategy(), 10, TimeSpan.FromSeconds(3))
        {

        }
    }
}
